package com.kkwrite.regimen.consumer.controller.point;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.kkwrite.regimen.common.constant.SortTypeEnum;
import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.point.PageablePointRecordOutDTO.OutData;
import com.kkwrite.regimen.consumer.controller.constant.Constant;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by lisha on 2018/7/21 16:33.
 *
 * @author lisha
 */
@Api(value = "积分模块")
@RestController
public class PointController {

	@Resource(name = "stringRedisTemplate")
	private StringRedisTemplate redisTemplate;
	@Resource(name = "restTemplate")
	private RestTemplate restTemplate;

	@GetMapping(value = "/point/queryrecords/{sortType}")
	@HystrixCommand(fallbackMethod = "queryPointRecordListError")
	@ApiOperation(value = "分页查询积分记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "获取第page页数据", required = true, allowableValues = "[0, infinity]", paramType = "query", dataType = "int"),
			@ApiImplicitParam(name = "size", value = "每页size条记录", required = true, allowableValues = "[1, infinity]", paramType = "query", dataType = "int"),
			@ApiImplicitParam(name = "sortType", value = "按时间排序的排序类型", defaultValue = "2", allowableValues = "{1, 2}", paramType = "path", dataType = "int") })
	public OutDTO queryPointRecordList(
			@RequestParam(required = false) Integer page,
			@RequestParam(required = false) Integer size,
			@PathVariable(required = false) Integer sortType,
			HttpServletRequest request) {
		// 参数校验
		String token = request.getHeader("token");
		Object userIdObj = redisTemplate.opsForHash().get(token, "userId");
		if (userIdObj == null) {
			return new OutDTO<>("false", "002", "token invalid");
		}
		if (page == null || page < 0 || size == null || size < 1) {
			return new OutDTO<>("false", "入参不合法");
		}
		if (!SortTypeEnum.isSortType(sortType)) {
			sortType = 2;
		}
		return restTemplate.getForObject(Constant.SERVICE_PREFIX
				+ "/point/recordList?page=" + page + "&size=" + size
				+ "&userId=" + userIdObj + "&sortType=" + sortType,
				OutDTO.class);
	}

	@GetMapping(value = "/point/queryPointTotal")
	@HystrixCommand(fallbackMethod = "queryPointTotalError")
	@ApiOperation(value = "查询总积分")
	public OutDTO queryPointTotal(HttpServletRequest request) {
		// 获取uesrId
		String token = request.getHeader("token");
		Object userId = redisTemplate.opsForHash().get(token, "userId");
		if (userId == null) {
			return new OutDTO<>("false", "002", "token invalid");
		}
		String url = Constant.SERVICE_PREFIX+ "/point/queryPointTotal?userId=" + userId;
		OutDTO outDto = restTemplate.getForObject(url, OutDTO.class);
		Map outMap = (Map)outDto.getData();
		outMap.remove("pointSource");
		outMap.remove("pointValue");
		outMap.remove("createTime");
		return outDto;
	}

	public OutDTO queryPointRecordListError(Integer page, Integer size,
			Integer sortType, HttpServletRequest request) {
		return new OutDTO<>("false", "服务异常！");
	}
	public OutDTO queryPointTotalError(HttpServletRequest request) {
		return new OutDTO<>("false", "服务异常！");
	}

}
